const e = require("express");
const { Order } = require("../models/orderModel");
//获取所有销售订单
async function getSalesOrder(req, res) {
    const { pageSize, currentPage, type, _id, value } = req.body;
    // 总条数
    const total = await (await Order.find({ salesId: _id })).length
    // 总页数
    const totalPages = Math.ceil(total / pageSize);
    let data;
    if (value) {
        data = await Order
            .find({
                $and:
                    [
                        { salesId: _id },
                        { [type]: value }
                    ]
            })
            .sort({name:-1})
            .limit(pageSize - 0)
            .skip((currentPage - 1) * pageSize)
            .populate("clientId")//关联客户
            .populate("salesId")//关联销售员
            .populate({
                //保险产品
                path: "insurancesId",
                //保险类型
                populate: { path: 'insuranceTypeId' }
            })
    } else {
        data = await Order
            .find({ salesId: _id },)
            .limit(pageSize - 0)
            .skip((currentPage - 1) * pageSize)
            .populate("clientId")//关联客户
            .populate("salesId")//关联销售员
            .populate({
                //保险产品
                path: "insurancesId",
                //保险类型
                populate: { path: 'insuranceTypeId' }
            })
    }

    if (data) {
        res.send({
            status: 1,
            data: { total, totalPages, data, pageSize, currentPage },
            message: '获取成功'
        })
    } else {
        res.send({
            status: 0,
            message: '获取失败'
        })
    }
}
//新增订单
async function addOrder(req, res) {
    try {
        // insurancesId  保险产品
        // clientId  客户id
        const { name, salesId, clientId, insurancesId } = req.body;
        console.log(salesId, clientId);
        const data = await Order.create({ name, salesId, clientId, insurancesId, claim: 0, status: '待生效' })
        res.send({
            status: 1,
            data,
            message: '新增成功'
        })
    } catch (error) {
        res.send({
            status: 0,
            message: '新增失败',
        })
    }
}
// 删除订单
async function deletOrder(req, res) {
    try {
        const { _id } = req.body;
        const data = await Order.findByIdAndDelete(_id)
        res.send({
            status: 1,
            data,
            message: '删除订单成功'
        })
    } catch (error) {
        res.send({
            status: 0,
            message: '删除订单失败'
        })
    }
}
// 获取一个订单
async function getOrder(req, res) {
    try {
        const { _id } = req.body;
        const data = await Order
            .findById(_id)
            .populate('clientId')
            .populate('salesId')
            .populate({
                //保险产品
                path: "insurancesId",
                //保险类型
                populate: { path: 'insuranceTypeId' }
            })
        res.send({
            status: 1,
            data,
            message: '获取订单成功'
        })
    } catch (error) {
        res.send({
            status: 0,
            message: '获取订单失败'
        })
    }
}
//修改订单
async function updateOrderSuccess(req, res) {
    try {
        const { _id, name, type, time, status } = req.body;
        console.log( _id, name, type, time, status);
        const data = await Order.updateOne({ _id }, { name, type, time, status })
        console.log('修改', data);
        res.send({
            status: 1,
            data,
            message: '修改订单成功'
        })
    } catch (error) {
        res.send({
            status: 0,
            data,
            message: '修改订单失败'
        })
    }
}
//查找订单
async function findTypeOrder(req, res) {
    const { type, value, pageSize = 10, currentPage = 1 } = req.body
    const data = await Order
        .find({ [type]: value })
        .limit(pageSize - 0)   // 设置请求的条数
        .skip((currentPage - 1) * pageSize)   // 设置跳过的条数
        .populate("clientId")//关联客户
        .populate("salesId")//关联销售员
        .populate({
            //保险产品
            path: "insurancesId",
            //保险类型
            populate: { path: 'insuranceTypeId' }
        })
    if (data) {
        res.send({
            status: 1,
            data: { total, totalPages, data, pageSize, currentPage },
            message: '获取成功'
        })
    } else {
        res.send({
            status: 0,
            message: '获取失败'
        })
    }
}
module.exports = { findTypeOrder, getSalesOrder, addOrder, deletOrder, getOrder, updateOrderSuccess }
